package com.magenta.maxoptra.geocoding.geoinformsputnik;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: Nikolay
 * Date: 07.02.12
 * Time: 12:34
 */
public class GeoHelper {
    static final String[] townsNotes = new String[]{
            "п.", "пос.", "поселок", "п ",
            "с.", "село", "c ",
            "г.", "город", "г ", "г,"};

    static final String[] townsNotesRegExp = new String[]{
            "п[ ]*\\.", "пос[ ]*\\.", "поселок", "п ",
            "с[ ]*\\.", "село", "c ",
            "г[ ]*\\.", "город", "г "};

    public static boolean isTownAddress(String address) {
        for (String townsNote : townsNotes) {
            if (address.indexOf(townsNote) == 0 || address.indexOf(" " + townsNote) >= 0) {
                return true;
            }
        }
        return false;
    }

    public static String getTown(String address) {
        address = address.toLowerCase();
        for (String townsNote : townsNotesRegExp) {
            final Pattern compile = Pattern.compile(townsNote + "[ ,]*(.[^ ,]*)[ ,]");

            final Matcher matcher = compile.matcher(address);
            if (matcher.find()) {
                return matcher.group(1);
            }
        }
        return null;
    }
}
